package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Api(tags = "文件上传")
@RestController
@RequestMapping("/admin/common")
public class CommonController {
    @Autowired
    AliOssUtil aliOssUtil;

    /*
     * 文件上传
     * */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        //1.接受请求参数，kv 形式，定义变量接受
        //2.调用AliOssUtil工具栏，实现文件上传
        //2.1随机生成文件名称
        String fileName = UUID.randomUUID().toString();
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileUrl = aliOssUtil.upload(file.getBytes(), fileName + suffix);

        //3.封装/返回处理对象

        return Result.success(fileUrl);
    }

}
